<template>
  <div class="upload-box">
    <el-upload
      action="#"
      list-type="picture-card"
      :http-request="uploadFn"
      :limit="1"
      :file-list="fileList"
      :class="{disable:fileList.length>0}"
      :on-remove="removeFn"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <!-- 进度条：当开始上传且未上传完成之前 显示
    percentage: 设置当前进度
     -->
    <el-progress v-if="percent>0&&percent<100" type="circle" :percentage="percent" />
  </div>
</template>
<script>
import COS from 'cos-js-sdk-v5'

// SECRETID 和 SECRETKEY请登录 https://console.cloud.tencent.com/cam/capi 进行查看和管理
var cos = new COS({
  SecretId: 'AKIDiGagXN6eUDk4CE7sRm4DG6i7MfkaEh3F',
  SecretKey: 'FKG4h9rdO5iDXhjeQ2Jg3JRRHwSp6CC5'
})
export default {
  components: {},
  data() {
    return {
      fileList: [],
      percent: 0
    }
  },
  computed: {},
  watch: {},
  methods: {
    // 移除图片
    removeFn() {
      this.fileList = []
      // console.log(111,file, fileList);
    },
    uploadFn(data) {
      // console.log(19,data);
      this.fileList = [{ url: '' }]
      cos.putObject(
        {
          Bucket: 'hr-renzi-1314002025' /* 填入您自己的存储桶，必须字段 */,
          Region: 'ap-guangzhou' /* 存储桶所在地域，例如ap-beijing，必须字段 */,
          Key: Date.now().toString() /* 存储在桶里的对象键（例如1.jpg，a/b/test.txt），必须字段 */,
          StorageClass: 'STANDARD',
          Body: data.file, // 上传文件对象
          onProgress: (progressData) => {
            // console.log(JSON.stringify(progressData));
            this.percent = progressData.percent * 100
          }
        }, (err, data) => {
          //   console.log(err, 42, data);
          if (!err) {
            // this.fileList.push({
            //   url: "https://" + data.Location
            // });
            this.fileList[0].url = 'https://' + data.Location
          }
        }
      )
    }
  }
}
</script>
<style lang="scss" scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
